package com.ruoyi.devops.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.devops.domain.InspectionAdg;
import com.ruoyi.devops.domain.InspectionTask;
import com.ruoyi.devops.domain.InspectionTemplate;
import com.ruoyi.devops.mapper.InspectionTaskMapper;
import com.ruoyi.devops.mapper.InspectionTemplateMapper;
import com.ruoyi.devops.service.IInspectionTaskService;
import com.ruoyi.devops.service.IInspectionTemplateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class InspectionTaskService extends ServiceImpl<InspectionTaskMapper, InspectionTask> implements IInspectionTaskService {
    @Resource
    InspectionTaskMapper inspectionTaskMapper;


    public AjaxResult queryInspectionTaskTemplate(Map<String,String> map) {
        QueryWrapper wrapper = new QueryWrapper<InspectionTask>();

        String task_name = map.get("taskName");
        String task_type = map.get("taskType");
        String patrol_start = map.get("patrolStart");

        String patrol_end = map.get("patrolEnd");


        if (StringUtils.isNotEmpty(task_name) && !"null".equals(task_name)) {
            wrapper.like("task_name",task_name);
        }
        if (StringUtils.isNotEmpty(task_type) && !"null".equals(task_type)) {
            wrapper.like("task_type",task_type);
        }
        if (StringUtils.isNotEmpty(patrol_start) && !"null".equals(patrol_start)) {

            patrol_start = patrol_start + " 00:00:00";
            wrapper.gt("patrol_start",patrol_start);
        }
        if (StringUtils.isNotEmpty(patrol_end) && !"null".equals(patrol_end)) {
            patrol_end = patrol_end + " 23:59:59";
            wrapper.lt("patrol_end",patrol_end);
        }
        wrapper.eq("status","0");

        wrapper.orderByDesc("patrol_start");

        int pp = Integer.parseInt(map.get("page"));

        int ss = Integer.parseInt(map.get("size"));

        List<InspectionTask> list = inspectionTaskMapper.selectList(wrapper);

        wrapper.last("limit "+(pp-1) * ss +"," + ss);

        List<InspectionTask> list1 = inspectionTaskMapper.selectList(wrapper);

        return AjaxResult.success(String.valueOf(list.size()),list1);
    }

    public InspectionTask queryInspectionTaskTemplateById(InspectionTask inspectionTask) {

        String id = inspectionTask.getId();

        InspectionTask re = inspectionTaskMapper.selectById(id);
        return re;
    }

    public int delInspectionTaskTemplate(InspectionTask inspectionTask) {
//        String id = inspectionTask.getId();
//        inspectionTask.setStatus("1");
        inspectionTask.setTaskStatus("禁用");
        return inspectionTaskMapper.updateById(inspectionTask);
    }

    public int updInspectionTaskTemplate(InspectionTask inspectionTask) {
//        String id = inspectionTask.getId();
        inspectionTask.setTaskStatus("启用");
        return inspectionTaskMapper.updateById(inspectionTask);
    }


    public int scInspectionTaskTemplate(InspectionTask inspectionTask) {
        StringBuffer stringBuffer = new StringBuffer("\n" +
                "巡检报告\n" +
                "日期：");
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        //System.out.println(dateFormat.format(calendar.getTime()));
        stringBuffer.append(dateFormat.format(calendar.getTime()));
        stringBuffer.append("\n" +
                "\n" +
                "任务名称：").append(inspectionTask.getTaskName()).append("\n" +
                "巡检人员：管理员").append("\n" +
                "\n" +
                "一、巡检概要：\n" +
                "在本次巡检中，对项目进行了全面的检查和评估。巡检的目的是确保项目的正常运行和安全性，发现潜在的问题和风险，并提出相应的建议和改进措施。");
        stringBuffer.append("二、巡检结果：\n" +
                "\n" +
                "系统状态：\n" +
                "\n" +
                "系统正常运行，并且各项功能均能够正常使用。\n" +
                "系统响应时间在可接受的范围内，用户体验良好。\n" +
                "未发现系统崩溃、错误或异常退出的情况。");
        inspectionTask.setTaskReport(stringBuffer.toString());
        String id = inspectionTask.getId();
        return inspectionTaskMapper.updateById(inspectionTask);
    }


    @Override
    public void copyInspectionTaskTemplate(InspectionTask inspectionTask) {
        inspectionTask.setId(UUID.randomUUID().toString());
       // inspectionTaskMapper.insert(inspectionTask);
        //sql 注入  修改insert 方法手写参数
        inspectionTaskMapper.insertOneObj(inspectionTask);
    }

//    @Override
//    public void copyInspectionTaskTemplate(Map<String, String> map) {
//
//    }
}
